package org.yang.order.food.service.impl;


import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.yang.order.food.common.Msg;
import org.yang.order.food.dao.FoodTypeMapper;
import org.yang.order.food.entity.FoodType;
import org.yang.order.food.service.FoodTypeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 菜品类型服务实现
 * @author shichenyang
 * @email gyv12345@163.com
 * @date 2018/12/03
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class FoodTypeServiceImpl implements FoodTypeService {
    @Resource
    private FoodTypeMapper foodTypeMapper;

    @CacheEvict(cacheNames="foodType",allEntries=true)
    @Override
    public Msg addFoodType(FoodType foodType) {
        int i=  foodTypeMapper.insert(foodType);
        Msg msg=new Msg();
        if (i>0){
            msg.setMsg("添加成功");
        }else{
            msg.setCode(-1);
            msg.setMsg("添加失败");
        }
        return msg;
    }
    @CacheEvict(cacheNames="foodType",allEntries=true)
    @Override
    public Msg updateFoodType(FoodType foodType) {
        int i= foodTypeMapper.updateByParam(foodType);
        Msg msg=new Msg();
        if (i>0){
            msg.setMsg("修改成功");
        }else{
            msg.setCode(-1);
            msg.setMsg("修改失败");
        }
        return msg;
    }

    @CacheEvict(cacheNames="foodType",allEntries=true)
    @Override
    public Msg delFoodType(FoodType foodType) {
        int i=foodTypeMapper.del(foodType.getId());
        Msg msg=new Msg();
        if (i>0){
            msg.setMsg("删除成功");
        }else{
            msg.setCode(-1);
            msg.setMsg("删除失败");
        }
        return msg;
    }

    @Cacheable(cacheNames = "foodType")
    @Override
    public List<FoodType> getFoodTypes(FoodType foodType) {
        List<FoodType> foodTypes=foodTypeMapper.list(foodType);
        return foodTypes;
    }
}
